package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.query.SoftQuery;
import cn.nubia.admin.application.service.SoftService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.application.bean.SearchGeneral;
import cn.nubia.admin.application.query.GeneralQuery;
import cn.nubia.admin.application.service.SearchGeneralService;
import cn.nubia.admin.application.query.TopicQueryParam;
import cn.nubia.admin.application.service.TopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by justin on 15/7/13.
 */
@Controller
@RequestMapping(value = "/admin/general/")
public class SearchGeneralController {
    @Autowired
    SearchGeneralService searchGeneralService;
    @Autowired
    SoftService softService;
    @Autowired
    TopicService topicService;

    @RequiresPermissions(value = "soft:rec:general:list")
    @RequestMapping(value = "list")
    public ModelAndView list(GeneralQuery generalQuery){
        ModelAndView modelAndView = new ModelAndView("/general/generalList");
        Paged<SearchGeneral> sgs = searchGeneralService.listPagedSearchGeneral(generalQuery);
        modelAndView.addObject("sgs",sgs);
        modelAndView.addObject("query",generalQuery);
        return modelAndView;
    }
    @RequestMapping(value = "add")
    public ModelAndView add(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("/general/generalForm");
        modelAndView.addObject("myReferer", request.getHeader("Referer"));
        modelAndView.addObject("sg",new SearchGeneral());
        return modelAndView;
    }
    @RequestMapping(value = "edit")
    public ModelAndView edit(Integer id,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("/general/generalForm");
        modelAndView.addObject("myReferer", request.getHeader("Referer"));
        SearchGeneral sg = searchGeneralService.getSearchGenenralById(id);
        if(sg.getGeneralType() == SearchGeneral.TYPE_SOFT){
            SoftQuery softQuery = new SoftQuery();
            softQuery.setId(sg.getResId());
            modelAndView.addObject("checkSofts",softService.findSoftList(softQuery));
        }else if(sg.getGeneralType() == SearchGeneral.TYPE_TOPIC){
            TopicQueryParam topicQuery = new TopicQueryParam();
            topicQuery.setTopicId(sg.getResId());
            modelAndView.addObject("topics", topicService.findPageList(topicQuery));
        }
        modelAndView.addObject("sg",sg);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public Response<Boolean> save(SearchGeneral searchGeneral, BindingResult result){
        if(result.hasErrors())return Response.getFailedResponse(result.getAllErrors().get(0).getDefaultMessage());
        if(searchGeneral.getGeneralType()==null ||searchGeneral.getGeneralType()<0)
            return Response.getFailedResponse("推广类型不能为空");
        if(searchGeneral.getBeginDate()==null)
            return Response.getFailedResponse("开始时间不能为空");

        //处理校验generalType
        if(searchGeneral.getGeneralType() ==SearchGeneral.TYPE_SOFT){
            searchGeneral.setResId(Integer.valueOf(searchGeneral.getSoftradio()));
        }else if(searchGeneral.getGeneralType() == SearchGeneral.TYPE_TOPIC){
            searchGeneral.setResId(Integer.valueOf(searchGeneral.getTopicradio()));
        }
        if(searchGeneral.getGeneralType()==SearchGeneral.TYPE_SEARCHWORD){
            if(searchGeneral.getGeneralWord()==null || "".equals(searchGeneral.getGeneralWord()))
                return Response.getFailedResponse("推广文字不能为空");
        }else if (searchGeneral.getGeneralType()==SearchGeneral.TYPE_LINK){
            if(searchGeneral.getLinkUrl()==null || "".equals(searchGeneral.getLinkUrl()))
                return Response.getFailedResponse("外部链接不能为空");
        }else{
            if(searchGeneral.getResId()==null)
                return Response.getFailedResponse("推广资源Id不能为空");
        }


        searchGeneralService.saveGeneral(searchGeneral);
        return Response.getSuccessResponse(true);
    }


    @RequestMapping(value = "delete")
    public String delete(Integer id){
        searchGeneralService.deleteSG(id);
        return "redirect:list.html";
    }
}
